﻿using System;
using System.Collections;
using System.Collections.Generic;
using jQube.Resources;

namespace jQube.Web.UI.Controls
{
    public class TriggerCollection : IList, IEnumerable<Trigger>
    {
        private UpdatePanel parent;
        private List<string> keys;
        private List<Trigger> list;

        public TriggerCollection(UpdatePanel parent)
        {
            this.parent = parent;
            this.list = new List<Trigger>();
            this.keys = new List<string>();
        }

        public UpdatePanel Parent
        {
            get
            {
                return this.parent;
            }
            set
            {
                this.parent = value;
            }
        }

        internal List<Trigger> List
        {
            get
            {
                return this.list;
            }
        }

        public int Add(Trigger value)
        {
            if (!this.Parent.IsDesignMode && keys.Contains(value.TriggerControlID))
            {
                throw new InvalidOperationException(
                    string.Format(AtlasWeb.UpdatePanel_TriggerControlIDAlreadyExists,
                    value.TriggerControlID,
                    this.Parent.ID));
            }

            this.list.Add(value);
            this.keys.Add(value.TriggerControlID);

            return this.list.Count - 1;
        }

        public void Clear()
        {
            this.list.Clear();
            this.keys.Clear();
        }

        public bool Contains(Trigger value)
        {
            return this.list.Contains(value);
        }

        public int IndexOf(Trigger value)
        {
            return this.list.IndexOf(value);
        }

        public void Insert(int index, Trigger value)
        {
            this.list.Insert(index, value);
            this.keys.Insert(index, value.TriggerControlID);
        }

        public void Remove(Trigger value)
        {
            int index = this.IndexOf(value);
            
            this.list.Remove(value);
            this.keys.RemoveAt(index);
        }

        public void RemoveAt(int index)
        {
            this.RemoveAt(index);
            this.keys.RemoveAt(index);
        }

        public Trigger this[int index]
        {
            get
            {
                return this.list[index];
            }
            set
            {
                this.list[index] = value;
            }
        }

        public void CopyTo(Trigger[] array, int index)
        {
            this.list.CopyTo(array, index);
        }

        public int Count
        {
            get
            {
                return this.list.Count;
            }
        }

        public IEnumerator<Trigger> GetEnumerator()
        {
            return new TriggerEnumerator(this);
        }

        #region Miembros de IList

        int IList.Add(object value)
        {
            if (value is Trigger)
                return this.Add(value as Trigger);
            else
                return -1;
        }

        void IList.Clear()
        {
            this.Clear();
        }

        bool IList.Contains(object value)
        {
            if (value is Trigger)
                return this.Contains(value as Trigger);
            else
                return false;
        }

        int IList.IndexOf(object value)
        {
            if (value is Trigger)
                return this.IndexOf(value as Trigger);
            else
                return -1;
        }

        void IList.Insert(int index, object value)
        {
            if (value is Trigger)
                this.Insert(index, value as Trigger);
        }

        bool IList.IsFixedSize
        {
            get { return false; }
        }

        bool IList.IsReadOnly
        {
            get { return false; }
        }

        void IList.Remove(object value)
        {
            if (value is Trigger)
                this.Remove(value as Trigger);
        }

        void IList.RemoveAt(int index)
        {
            this.RemoveAt(index);
        }

        object IList.this[int index]
        {
            get
            {
                return this.list[index];
            }
            set
            {
                throw new InvalidOperationException("The collection is read-olny");
            }
        }

        #endregion

        #region Miembros de ICollection

        void ICollection.CopyTo(Array array, int index)
        {
            if (array is Trigger[])
                this.CopyTo(array as Trigger[], index);
        }

        int ICollection.Count
        {
            get { return this.Count; }
        }

        bool ICollection.IsSynchronized
        {
            get { return false; }
        }

        object ICollection.SyncRoot
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region Miembros de IEnumerable

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}
